package com.hsnn.medstgmini.complain.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.complain.model.SysComplain;
import com.hsnn.medstgmini.complain.service.SysComplainAttachmentManager;
import com.hsnn.medstgmini.complain.service.SysComplainDelcompManager;
import com.hsnn.medstgmini.complain.service.SysComplainHospManager;
import com.hsnn.medstgmini.complain.service.SysComplainManager;
import com.hsnn.medstgmini.complain.service.SysComplainProdManager;
import com.hsnn.medstgmini.complain.service.SysComplainProdcompManager;
import com.hsnn.medstgmini.complain.service.SysComplainProjManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;

@Controller
@RequestMapping("/sysComplain")
public class SysComplainController {
	private static final Logger log = Logger.getLogger(SysComplainController.class);
	
	
	@Autowired
	private SysComplainManager sysComplainManager ;
	
	@Autowired
	private SysComplainProjManager sysComplainProjManager;
	
	@Autowired
	private SysComplainHospManager sysComplainHospManager;
	
	@Autowired
	private SysComplainDelcompManager sysComplainDelcompManager;
	
	@Autowired
	private SysComplainProdcompManager sysComplainProdcompManager;
	
	@Autowired
	private SysComplainProdManager sysComplainProdManager;
	
	@Autowired
	private SysComplainAttachmentManager sysComplainAttachmentManager;
	
	@Autowired
	private HttpServletRequest request;
	/**
	 * @category  跳转到申投诉列表页面
	 * @author 韩守松
	 * @date   2015年9月8日
	 * @param  @return
	 */
	@RequestMapping("/toSysComplainListJsp")
	public String toSysComplainListJsp(){
		return "/complain/list";
	}
	
	
	@RequestMapping("/getSysComplainDataList")
	@ResponseBody
	public Pagination getSysComplainDataList(SysComplain sysComplain){
		Pagination page = new Pagination(request);
		try{
		SysUser user = (SysUser)request.getSession().getAttribute(Constants.USERINFO);
		sysComplain.setComplainProdcomp(user.getId());
		page=sysComplainManager.getSysComplainDataList(sysComplain);
		page.setSuccess(true);
		}catch(Exception e){
			log.equals("生产企业申投诉信息获取失败");
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
	/**
	 * @category 跳转到申投诉详情页面
	 * @author 韩守松
	 * @date   2015年9月9日
	 * @param  @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/toSysComplaindetailJsp")
	public String toSysComplaindetailJsp(String complainId){
		List list =  sysComplainManager.getSysComplainById(complainId);
		request.setAttribute("list", list.get(0));
		List listProj = sysComplainProjManager.getSysComplainProjById(complainId);
		request.setAttribute("listProj", listProj);
		List listHosp = sysComplainHospManager.getSysComplainHospById(complainId);
		request.setAttribute("listHosp", listHosp);
		List listDelcomp = sysComplainDelcompManager.getSysComplainDelcompById(complainId);
		request.setAttribute("listDelcomp", listDelcomp);
		List listProdcomp = sysComplainProdcompManager.getSysComplainProdcompById(complainId);
		request.setAttribute("listProdcomp", listProdcomp);
		List listProd = sysComplainProdManager.getSysComplainProdById(complainId);
		request.setAttribute("listProd", listProd);
		List listAttachment = sysComplainAttachmentManager.getSysComplainById(complainId);
		request.setAttribute("listAttachment", listAttachment);
		return "/complain/detail";
	}
	
	/**
	 * @category  跳转到中心 申投诉处理页面
	 * @author 韩守松
	 * @date   2015年9月8日
	 * @param  @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("/toSysComplainHandleJsp")
	public String toSysComplainHandleJsp(String complainId){
		List list =  sysComplainManager.getSysComplainById(complainId);
		request.setAttribute("list", list.get(0));
		List listProj = sysComplainProjManager.getSysComplainProjById(complainId);
		request.setAttribute("listProj", listProj);
		List listHosp = sysComplainHospManager.getSysComplainHospById(complainId);
		request.setAttribute("listHosp", listHosp);
		List listDelcomp = sysComplainDelcompManager.getSysComplainDelcompById(complainId);
		request.setAttribute("listDelcomp", listDelcomp);
		List listProdcomp = sysComplainProdcompManager.getSysComplainProdcompById(complainId);
		request.setAttribute("listProdcomp", listProdcomp);
		List listProd = sysComplainProdManager.getSysComplainProdById(complainId);
		request.setAttribute("listProd", listProd);
		List listAttachment = sysComplainAttachmentManager.getSysComplainById(complainId);
		request.setAttribute("listAttachment", listAttachment);
		return "/complain/handle";
	}
	
	/**
	 * @category  申投诉 处理
	 * @author 韩守松
	 * @date   2015年9月14日
	 * @param  @return
	 */
	@RequestMapping("/updataSysComplain")
	@ResponseBody
	public Pagination updataSysComplain(SysComplain sysComplain){
		Pagination page = new Pagination(request);
		try{
			SysUser user =  (SysUser)request.getSession().getAttribute(Constants.USERINFO);
			sysComplain.setUserType(user.getType());
			sysComplain.setProcessUsername(user.getId());
			sysComplain.setProcessDatetime(new Date());
			sysComplain.setUpdUser(user.getId());
			sysComplain.setUpdDatetime(new Date());
		    sysComplainManager.updata(sysComplain);
			page.setSuccess(true);
		}catch(Exception e){
			log.equals("更新申投诉信息失败"+e);
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 用来弹出配送企业选择页面
	 * @author 韩守松
	 * @date   2015年9月15日
	 * @param  @return
	 */
	@RequestMapping("/toDelcompListJsp")
	public String toDelcompListJsp(Integer numberId,String delcompId){
		request.setAttribute("numberId", numberId);
		request.setAttribute("delcompId", delcompId);
		return "/complain/delcomp";
	}
	
	
	/**
	 * @category 用来弹出生产企业选择页面
	 * @author 韩守松
	 * @date   2015年9月15日
	 * @param  @return
	 */
	@RequestMapping("/toProdcompListJsp")
	public String toProdcompListJsp(Integer numberId,String prodcompId){
		request.setAttribute("numberId", numberId);
		request.setAttribute("prodcompId", prodcompId);
		return "/complain/prodcomp";
	}
	
	/**
	 * @category 用来弹出项目选择页面
	 * @author 韩守松
	 * @date   2015年9月15日
	 * @param  @return
	 */
	@RequestMapping("/toProjListJsp")
	public String toProjListJsp(Integer numberId,String projId){
		request.setAttribute("numberId", numberId);
		request.setAttribute("projId", projId);
		return "/complain/proj";
	}
	/**
	 * @category 用来弹出医疗机构选择页面
	 * @author 韩守松
	 * @date   2015年9月15日
	 * @param  @return
	 */
	@RequestMapping("/toHospListJsp")
	public String toHospListJsp(Integer numberId,String hospId){
		request.setAttribute("numberId", numberId);
		request.setAttribute("hospId", hospId);
		return "/complain/hosp";
	}
	/**
	 * @category 用来弹出药品选择页面
	 * @author 韩守松
	 * @date   2015年9月15日
	 * @param  @return
	 */
	@RequestMapping("/toProdListJsp")
	public String toProdListJsp(Integer numberId,String prodId){
		request.setAttribute("numberId", numberId);
		request.setAttribute("prodId", prodId);
		return "/complain/prod";
	}
}
